// pages/ownerOertification/ownerOertification.js

var app = getApp();
var util = require('../../utils/util.js');
var applet_park_api_url = app.globalData.applet_park_api_url;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nickName:'',
    proprietorName: '',
    proprietorPhone: '',
    roomNumber: '',
    houseArea: '',
    listarea: [],
    listareaid: [],
    listareaindex: 0,
    listbuild: ['请选择'],
    listbuildid: [null],
    listbuildindex: 0,
    listfloor: [],
    listfloorid: [],
    listfloorindex: 0,
    Positive: '',
    unPositive: '',
    detailInfo: {
      pic1: null,
      pic2: null,
    },
    areaId: '',
    floorsid:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var userInfo = wx.getStorageSync("USER_INFO");
    if (userInfo){
      this.setData({

        phone: userInfo.bindPhone
      });
    }
    var nickName = wx.getStorageSync("nickName");
    if (nickName){
      this.setData({
        nickName: nickName
      
      });
    }
 
    //
    this.loadbuild();
  },
  //选择
  changeUsedType: function (e) {
    
    if (!this.data.buildid || this.data.buildid == 'null' || this.data.buildid == null) {
      util.showModal('请先选择楼栋');
      return
    }
    this.data.areaId = this.data.listareaid[e.detail.value];
    this.data.listareaindex = e.detail.value;
    this.setData(this.data);
    this.loadFloor();
    
  },
  changefloor: function (e) {
    if (!this.data.areaId || this.data.areaId == 'null' || this.data.areaId == null) {
      util.showModal('请先选择楼层');
      return
    }
    
    this.data.floorId = this.data.listfloorid[e.detail.value];
    this.data.listfloorindex = e.detail.value;
    this.setData(this.data);

  },
  bindKeyInput: function (e) {
    this.data[e.currentTarget.dataset.inputname] = e.detail.value;
    this.setData(this.data);
  },
  changebuild: function (e) {
    
    this.data.buildid = this.data.listbuildid[e.detail.value];
    this.data.listbuildindex = e.detail.value;
    // if (e.detail.value < this.data.areslenth) {
    //   this.setData({
    //     addressshow: false
    //   })
    // }
    this.setData(this.data);
    this.loadAreas();
  },

  //楼栋
  loadbuild() {
    var that = this;
    wx.request({
      url: applet_park_api_url + '/api/buildings',
      success(res) {
        
        let listbuild = [];
        listbuild.push("请选择楼栋")
        let listbuildid = [];
        listbuildid.push("null")
        if (res.data.code == 0) {
          for (let item of res.data.data) {
            
            listbuild.push(item.name)
            listbuildid.push(item.id)
          }
          

          that.data.listbuild = listbuild;
          that.data.listbuildid = listbuildid;
          that.setData(that.data);
        } else {
          wx.showToast({
            title: '网络异常',
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: res.data,
        })
      }
    })
  },
  //加载区域
  loadAreas() {
    var that = this;
    wx.request({
      url: applet_park_api_url + '/api/floors?buildingId=' + that.data.buildid,
      success(res) {
        
        let listarea = [];
        listarea.push("请选择楼层")
        let listareaid = [];
        listareaid.push("null")
        if (res.data.code == 0) {
          for (let item of res.data.data) {
            
            listarea.push(item.name)
            listareaid.push(item.id)
          }
          

          that.data.listarea = listarea;
          that.data.listareaid = listareaid;
          that.setData(that.data);
        } else {
          // wx.showToast({
          //   title: '网络异常',
          // })
        }
      },
      fail(res) {
        wx.showToast({
          title: res.data,
        })
      }
    })
  },
  loadFloor() {
    var that = this;
    wx.request({
      url: applet_park_api_url + '/api/rooms?buildingId=' + that.data.areaId,
      success(res) {
        
        let listfloor = [];
        listfloor.push("请选择房间号")
        let listfloorid = [];
        listfloorid.push("null")
        if (res.data.code == 0) {
          for (let item of res.data.data) {
            
            listfloor.push(item.name)
            listfloorid.push(item.id)
          }
          

          that.data.listfloor = listfloor;
          that.data.listfloorid = listfloorid;
          that.setData(that.data);
        } else {
          // wx.showToast({
          //   title: res.data.mesage,
          // })
          util.showModal(res.data.mesage);
        }
      },
      fail(res) {
        wx.showToast({
          title: res.data.message
        })
      }
    })
  },
  submit() {//提交成功后获取用户信息
    let that = this;
    if (!that.data.nickName) {
      util.showModal('请选填写姓名');
      return
    }
    if (!that.data.phone) {
      util.showModal('请选填写电话');
      return
    }
    if (!that.data.areaId || (that.data.areaId === null)) {
      util.showModal('请选择楼层');
      return
    }
    if (!that.data.buildid || (that.data.buildid === null)) {
      util.showModal('请x选择楼栋');
      return
    }
   
    if (!that.data.floorId || (that.data.floorId === null)) {
      util.showModal('请x选择房间');
      return
    }
    var id = app.globalData.id || wx.getStorageSync("id");
    var openId = app.globalData.openId || wx.getStorageSync("OPENID");
    var userInfo = app.globalData.userInfo || wx.getStorageSync("USER_INFO");
    wx.request({
      url: applet_park_api_url + '/api/tenants/applyCertificate',
      method: 'POSt',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        buildingId: that.data.buildid,
        floorId: that.data.areaId,
        roomId: that.data.floorId,
        nickName: that.data.nickName,
        phone: that.data.phone,
        wxOpenId: openId,
      },
      success(res) {
        if (res.data.code== 0) {
          wx.showToast({
            title: "提交成功",
          })
          
            userInfo.bindPhone = res.data.data.mobile;
            userInfo.nickName = res.data.data.nickName;
            wx.setStorageSync("USER_INFO", userInfo);
            wx.setStorageSync("nickName", res.data.data.nickName);
            wx.setStorageSync("id", res.data.data.id);
            wx.setStorageSync("userRoles", res.data.data.userRoles);
            app.globalData.userRoles = res.data.data.userRoles;//保存角色
            app.globalData.id = res.data.data.id;//保存用户id
            app.globalData.nickName = res.data.data.nickName;//保存用户昵称
            app.globalData.userInfo = res.data.data.userInfo;//保存用户信息
            
            wx.redirectTo({
              url: '../index/index',
            })  
        } else {
          wx.showToast({
            title: res.data.message,
            icon:'none',
            duration:3000
          })
        }
      },
      fail() {

      }
    })


  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

})